<?php if ( ! defined('BASEPATH')) exit('This file does not run directly. Sorry.'); ?>
<?php
/**
 * Kalaisoo.
 *
 * A lightweight no-framework framework written in PHP.
 *
 * @package Kalaisoo
 * @author $Author: info@sah-company.com $
 * @version $Id: todo.php 151 2012-02-01 01:08:39Z info@sah-company.com $
 */

/**
 * Load dependencies.
 */
Registry::instance()->loader->model(array(
	'person'
	)
);

/**
 * Kalaisoo Todo Model Class.
 *
 * @package Kalaisoo
 * @subpackage Model
 * @author $Author: info@sah-company.com $
 */
class Model_Todo extends Model
{
	/**
	 * default order field and direction
	 *
	 * @var array
	 */
	public $order = array(
		'field' => 'todo_due_date',
		'dir' => 'ASC'
	);
	
	/**
	 * the default filter to apply
	 *
	 * @var string
	 */
	public $filter = 'open';
	
	/**
	 * all filters
	 *
	 * @var array
	 */
	public $filters = array();

	/**
	 * __construct.
	 */
	public function construct()
	{
		parent::__construct();
	}
	
	/**
	 * returns the default order clause for sql queries
	 *
	 * @return array
	 */
	public function orderClause()
	{
		return 'todo_due_date ASC, todo_summary ASC';
		//return $this->order['field'].' '.$this->order['dir'];
	}
	
	/**
	 * returns the tags of a bean.
	 *
	 * @return array
	 */
	public function getTags() {
		return array(
			$this->bean->summary
		);
	}
	
	/**
	 * loads the array $this->gestalt of this bean for use with Model::gestalt().
	 */
	public function loadGestalten()
	{
		$this->gestalt['default'] = array(
			array(
				'type' => 'hidden',
				'name' => 'finished'
			),
			array(
				'type' => 'datetime',
				'name' => 'completion_date',
				'help' => __('todo_completion_date_help'),
				'parameters' => array(
					'readonly' => 'readonly'
				)
			),
			array(
				'type' => 'date',
				'name' => 'due_date',
				'parameters' => array(
				)
			),
			array(
				'type' => 'select',
				'name' => 'priority',
				'parameters' => array(
					'options' => array(
						'none' => __('todo_no_priority'),
						'low' => __('todo_low_priority'),
						'medium' => __('todo_medium_priority'),
						'high' => __('todo_high_priority')
					)
				)
			),
			array(
				'type' => 'text',
				'name' => 'summary',
				'parameters' => array(
					'placeholder' => __('todo_summary_placeholder'),
					'required' => 'required'
				)
			),
			array(
				'type' => 'textarea',
				'name' => 'description',
				'help' => __('todo_description_help'),
				'parameters' => array(
					'placeholder' => __('todo_description_placeholder'),
					'cols' => 60,
					'rows' => 8
				)
			),
			array(
				'type' => 'nested',
				'name' => 'executive',
				'help' => __('todo_executive_help'),
				'parameters' => array(
					'alias' => 'person',
					'select' => 'name'
				)
			),
			array(
				'type' => 'url',
				'name' => 'url',
				'help' => __('todo_url_help'),
				'parameters' => array(
					'placeholder' => __('todo_url_placeholder'),
				)
			)
		);
	}
	
	/**
	 * returns an array with special actions the model can take.
	 *
	 * @return array
	 */
	public function actions()
	{
		if ( ! $this->bean->getId()) return array();
		if ($this->bean->completion_date != '0000-00-00 00:00:00') {
			return array(
				'incomplete' => true
			);
		}
		return array(
			'complete' => true
		);
	}
	
	/**
	 * validates the data and returns either true or false.
	 *
	 * @return bool
	 */
	public function validate()
	{
		$valid = true;
		return $valid;
	}
	
	/**
	 * sets completion date on this todo, update entry and autoBlog about completion.
	 *
	 * @return void
	 */
	public function complete()
	{
		try {
			$this->bean->finished = 1;
			$this->bean->completion_date = date('Y-m-d H:i:s');
			$this->controller()->entry->setMeta('tainted', 1);
			$this->controller()->entry->modification_user = $this->controller()->user();
			R::store($this->bean);
			R::store($this->controller()->entry);
			$this->controller()->user()->autoBlog($this->bean, 'complete');
			$this->controller()->data['messages'][] = __('todo_complete_success');
		} catch (Exception $e) {
			$this->controller()->data['errors'][] = __('todo_complete_failed');
		}
		return $this->controller()->view($this->bean->getMeta('type'), $this->bean->getId());
	}

	/**
	 * resets completion date on this todo, update entry and autoBlog about revocation.
	 *
	 * @return bool
	 */
	public function incomplete()
	{
		try {
			$this->bean->finished = 0;
			$this->bean->completion_date = '0000-00-00 00:00:00';
			$this->controller()->entry->setMeta('tainted', 1);
			$this->controller()->entry->modification_user = $this->controller()->user();
			R::store($this->bean);
			R::store($this->controller()->entry);
			$this->controller()->user()->autoBlog($this->bean, 'incomplete');
			$this->controller()->data['messages'][] = __('todo_incomplete_success');
		} catch (Exception $e) {
			$this->controller()->data['errors'][] = __('todo_incomplete_failed');
		}
		return $this->controller()->view($this->bean->getMeta('type'), $this->bean->getId());
	}

	/**
	 * open.
	 */
	public function open()
	{
	}

	/**
	 * dispense.
	 */
	public function dispense()
	{
		$this->bean->setMeta('buildcommand.unique', array(array('summary')));
		$this->filters = array(
			'open' => array(
				'label' => __('todo_filter_open'),
				'where' => 'todo_finished = 0'
			),
			'finished' => array(
				'label' => __('todo_filter_finished'),
				'where' => 'todo_finished = 1'
			)
		);
	}

	/**
	 * update.
	 */
	public function update()
	{
		if ( ! $this->bean->getId()) {
			$this->bean->finished = 0;
			$this->bean->due_date = date('Y-m-d'); // today
		}
	}
	
	/**
	 * after_update.
	 */
	public function after_update()
	{
	}
	
	/**
	 * delete.
	 */
	public function delete()
	{
	}
	
	/**
	 * after_delete.
	 */
	public function after_delete()
	{
	}
}
?>